<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=Stylesheet href=printPage.css />

<!-- 打印设置 -->
<style type="text/css" media=print>
#header,.top_title,#jqprint,#footer,#cssprint{display:none}
 
.Noprint{display:none}
.Noboder{border: 0px solid #000;margin:0px;height:10px;width:auto;}
.pages{border: 0px solid #000; margin:0px;}
.Minprint{height : auto; width:277mm;	border: 0px solid #666666; }
.PageNext{page-break-after: always;}
.Hidden{visibility:hidden;}

</style>
<script src="../js/jquery-1.12.4.min.js"	type="text/javascript"></script>
<script src="../js/json2.js"type="text/javascript"></script>
<script type="text/javascript">
var papersSize={
		A4:'210,297',
		A3:'297,420',
		B5:'176,250'
}
var printObj;
$(function() {
	printObj=window.opener.window.printObj;
	if(printObj==null){
		alert("无法获取数据，请联系代理通管理员！");
		return;
	}
	/*
	*开始设置页面格式
	var printObj={
			paper:'A4',
			xy:'y',
			paddingTop: 10,
			paddingRight:20,
			paddingBottom:10,
			paddingLeft:20,
			fontSize:12,
			title:'数据未获取成功',
			pageHead:'<table><tr><td>数据未获取成功</td></tr></table>',
			pageFoot:'<table><tr><td>数据未获取成功</td></tr></table>',
			tableHead:'<tr><td>数据未获取成功</td></tr>',
			data:null,
			pageSize:9
		}
	*/
    _default();

});
function _default(){
	$("#paper").val(printObj.paper);
	$("#top").val(printObj.paddingTop);
	$("#left").val(printObj.paddingLeft);
	$("#right").val(printObj.paddingRight);
	$("#bottom").val(printObj.paddingBottom);
	$("#pageSize").val(printObj.pageSize);
	$("#fontSize").val(printObj.fontSize);
	printObj.xy=='x'?$("#x").attr("checked",true):$("#y").attr("checked",true);
	format();
}
function format(){
	empty();
	setData();
	var xy=eval('papersSize.'+$("#paper").val());
	$(".pages").css('width',($('input[id="x"]:checked').val()=='x'?xy.split(',')[0]:xy.split(',')[1])-$("#left").val()-$("#right").val()+'mm');
	$(".pages").css('height',($('input[id="y"]:checked').val()=='y'?xy.split(',')[0]:xy.split(',')[1])-$("#top").val()-$("#bottom").val()-2+'mm');
	$(".pages").css('padding',$("#top").val()+'mm '+$("#right").val()+'mm '+$("#bottom").val()+'mm '+$("#left").val()+'mm');
	$(".pages").css('font-size',$("#fontSize").val()+'px');
	$(".tabborder").css('table-layout',	printObj.tableLayout);
	$("#tab_total").css('height',$(".pages").css('height').substring(0,$(".pages").css('height').length-2)-25+'mm');
}
function empty(){
	$(".printPaper").html('');
}
function setData(){	
	var dataSourse=printObj.data;
	var columns=printObj.columns;
	var data=new Array();
	for(var i=0;i<dataSourse.length;i++){
		var contentArray = new Array();
		for(var j=0; j<columns.length ; j++ ){
			if(columns[j]._hide) continue;
			var html=" ";
			var render=printObj.columns[j].render;
			if(render!=null){
				html = render(dataSourse[i]);
			}else
				html=dataSourse[i][columns[j].columnname]?dataSourse[i][columns[j].columnname]:" "
			contentArray.push(html);//根据前台的列名进行排序，可以继续改进，加上隐藏与否的校验。目前没有
		}
		data[i]=contentArray;
	}
	var maxPage=0;
	for(var i = 0;i<data.length;i++){
		var page=parseInt(i/$("#pageSize").val());
		maxPage=page;
		var nextPage=page*1+1;
		if(i%$("#pageSize").val()==0){
			var pageDivClass='pages PageNext';
			if(i>=data.length&&printObj.totalPage==null){
				pageDivClass='pages';
			}
			$('<div class="'+pageDivClass+'" id="pages_'+page+'">'+
		    '<div class="title" ></div>'+
		    printObj.pageHead+
		     '<table id="pages_tb_'+page+'" border="0" cellpadding="0" cellspacing="0" class="tabborder"></table>'+
		     '<div class="foot" id="pages_foot_'+page+'">'+
		     printObj.pageFoot+'</div></div>').appendTo($('.printPaper'));
			$(printObj.tableHead).appendTo($("#pages_tb_"+page));
			$("#pages_foot_"+page).find('#page').html(nextPage);
			
		}
		nowTable = document.getElementById("pages_tb_"+page);
		addRow(nowTable,data[i]);
	}
	
	if(printObj.totalRow!=null){
		$('<tr>'+printObj.totalRow+'</tr>').appendTo($('#pages_tb_'+maxPage));
	}
	if(printObj.totalPage!=null){
		$('<div class="pages" id="pages_total"><div class="title" ></div>'+
				printObj.pageHead+
				'<table class="tabborder td_c" id="tab_total"  border="0" cellpadding="0" cellspacing="0" ><tr><td height="38" align="center" valign="top">'+
				printObj.totalPage.replace(';"',';" class="td_unborder_r" align="center" border=0 cellpadding=0 cellspacing=0 ')
				+'<br/> <div style="text-align: left; padding:15px;">备注栏：</div> </td></tr></table>'
				).appendTo($('.printPaper'));
	}
	$('.maxPage').html(maxPage+1);
	$(".title").html(printObj.title);
	$('.nowDate').html(nowDate("yyyy年MM月dd日", 0, 0, 0));
}
function addRow(table,contentArray){
	var tableRow=table.insertRow(table.rows.length);
	tableRow.id = "tableRow_"+(table.rows.length-1);
	for(var i = 0;i<contentArray.length;i++){
		var cell = tableRow.insertCell(i);
		cell.innerHTML = contentArray[i];
	}
}
function insetRow(table,rowIndex,contentArray){
	var tableRow=table.insertRow(rowIndex);
	tableRow.id = "tableRow_"+rowIndex;
	for(var i = 0;i<contentArray.length;i++){
		var cell = tableRow.insertCell(i);
		cell.innerHTML = contentArray[i];
	}
}
function nowDate(formate,addYear,addMonth,addDate,showTime){
	var now=new Date();
	var year=now.getFullYear()+addYear;
	var month=now.getMonth()+1+addMonth;
	month="0"+month;
	month=month.substring(month.length-2);
	var day=now.getDate()+addDate;
	day="0"+day;
	day=day.substring(day.length-2);
	if(showTime){
		var hour="0"+now.getHours();
		hour=hour.substring(hour.length-2, hour.length);
		var minute="0"+now.getMinutes();
		minute=minute.substring(minute.length-2, minute.length);
		day+=(" "+hour+":"+minute);
	}
	if(formate=='-')
		return year+"-"+month+"-"+day;

	else
		return year + "年" + month + "月" + day + "日";
	
};
function printIE(){
	//$(".pages").css('height','auto');多减了2mm以防空白页问题
	window.print();
	//$(".pages").css('height',($('input[id="y"]:checked').val()=='y'?xy.split(',')[0]:xy.split(',')[1])-$("#top").val()-$("#bottom").val()+'mm');
}
</script>
</head>
<body style="background-color: #CCCCCC;">
<div class="tool Noprint">
  <table width="1000" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td width="67" rowspan="2" align="right">打印设置</td>
      <td width="68"  align="right"><label for="paper">纸张</label></td>
      <td width="151" colspan="3"><select name="paper" class="paper" id="paper">
          <option value="A4">A4(210mm×297mm) </option>
          <option value="A3">A3(297mm×420mm) </option>
          <option value="B5">B5(176mm×250mm) </option>
        </select></td>
      <td width="48" align="right"><input type="radio" name="radio" id="y" value="y" /></td>
      <td width="51" align="left" ><label for="y">横向</label></td>
      <td width="24" align="left">&nbsp;</td>
      <td width="59" align="right"><label for="top">上边距</label></td>
      <td width="71"><input name="top" type="text" class="padding" id="top" />
        mm</td>
      <td width="55" align="right">下边距</td>
      <td width="66" align="left"><input name="bottom" type="text" class="padding" id="bottom" />
        mm</td>
    </tr>
    <tr>
      <td align="right">每页</td>
      <td width="55"><input name="pageSize" type="text" class="padding" id="pageSize" />
      条</td>
      <td width="50" align="right">字号</td>
      <td width="46"><select name="fontSize" class="paper" id="fontSize">
        <option value="14">14</option>
        <option value="12">12</option>
        <option value="10">10</option>
        <option value="9">9</option>
      </select></td>
      <td align="right"><input type="radio" name="radio" id="x" value="x" /></td>
      <td align="left"><label for="x">纵向</label></td>
      <td>&nbsp;</td>
      <td align="right"><label for="left">左边距</label></td>
      <td><input name="left" type="text" class="padding" id="left" />
        mm</td>
      <td align="right">右边距</td>
      <td align="left"><input name="right" type="text" class="padding" id="right" />
        mm</td>
    </tr>
    <tr>
      <td colspan="12" align="center">
      <input name="reset" type="button" class="bt" value="恢复默认" onclick='_default();' />
      <input name="reset" type="button" class="bt" value="应用新格式"  onclick='format();'/>
        <input name="print" type="button" class="bt" value="开始打印" onclick="printIE();"/></td>
    </tr>
  </table>
  <br/>
</div>
<div style="height:100px" class="Noprint"></div>
<div class="printPaper Noboder">
 
</div>
</body>

</html>
